package com.bigdata.assignment.problem2;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

public class HashPartitioner extends Partitioner<Text, IntWritable> {

    @Override
    public int getPartition(Text key, IntWritable value, int numPartitions) {
        if (numPartitions == 0)
            return 0;
        if (key == null)
            return 0;

        // 对哈希值取绝对值后取模，避免负号影响
        int hashCode = Math.abs(key.hashCode());
        return hashCode % numPartitions;
    }

}
